package com.feeyin.radar.crm.dao.impl;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.ContractCaseDAO;
import com.feeyin.radar.crm.dao.ContractDAO;
import com.feeyin.radar.crm.model.CodeStore;
import com.feeyin.radar.crm.model.Contract;
import com.feeyin.radar.crm.model.ContractCase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class ContractDAOImpl extends SqlMapClientDaoSupport
  implements ContractDAO
{
	private ContractCaseDAO contractCaseDAO;
  public BigDecimal getUsersAllContractDetailGrossCost(Contract contract)
  {
    return (BigDecimal)getSqlMapClientTemplate().queryForObject("getUsersAllContractDetailGrossCost", contract);
  }

  public BigDecimal getUsersAllContractGrossBackCost(Contract contract) {
    return (BigDecimal)getSqlMapClientTemplate().queryForObject("getUsersAllContractGrossBackCost", contract);
  }

  public List getContractByCustomerId(Contract contract) {
    return getSqlMapClientTemplate().queryForList("getContractsByDynamic", contract);
  }

  public List getEveryMonthNewContractCount(Contract contract) {
    return getSqlMapClientTemplate().queryForList("getEveryMonthNewContractCount", contract);
  }

  public List getTotalYearNewContractCount(Contract contract) {
    return getSqlMapClientTemplate().queryForList("getTotalYearNewContractCount", contract);
  }

  public Integer getCountForContractByCustomer(Contract contract) {
    return (Integer)getSqlMapClientTemplate().queryForObject("getCountContractBy3Table", contract);
  }

  public List getContractInfoByUserId(Contract contract) {
    return getSqlMapClientTemplate().queryForList("getContractInfoByUserId", contract);
  }

  public Integer getCountForContractByUsers(Contract contract) {
    return (Integer)getSqlMapClientTemplate().queryForObject("getCountForContractByUsers", contract);
  }

  public List getContractByUsers(Contract contract) {
    return getSqlMapClientTemplate().queryForList("getContractByUsers", contract);
  }

  public List getContractByCustomer(Contract contract) {
    return getSqlMapClientTemplate().queryForList("getContractBy3Table", contract);
  }

  public List getAllContracts() {
    return getSqlMapClientTemplate().queryForList("getContractsByDynamic");
  }

  public List getContractsByDynamic(Contract contract) {
    if (contract == null) return null;
    return getSqlMapClientTemplate().queryForList("getContractsByDynamic", contract);
  }

  public List getPageContractsByDynamic(Contract contract, Page page) {
    if (contract == null) return null;
    contract.setPageStartRow(page.getPageStartRow());
    contract.setPageEndRow(page.getPageEndRow());
    return getSqlMapClientTemplate().queryForList("getPageContractsByDynamic", contract);
  }

  public List getContractsByCustomerId(Integer customerId) {
    if (customerId == null) return null;
    Contract contract = new Contract();
    contract.setCustomerId(customerId);
    return getSqlMapClientTemplate().queryForList("getContractsByDynamic", contract);
  }

  public Integer getPageContractsCountByDynamic(Contract contract) {
    if (contract == null) return null;
    return (Integer)getSqlMapClientTemplate().queryForObject("getPageContractsCountByDynamic", contract);
  }

  public List getTop5ContractOrBackCostOrGrossProfit(String assignPeriod) {
    if (StringUtils.isEmpty(assignPeriod)) return null;
    Date startDate = DateUtils.getCurrentYearFirstDay();
    Date endDate = DateUtils.getCurrentYearEndDay();

    if ("1".equals(assignPeriod)) {
      startDate = DateUtils.getCurrentMonthFirstDay();
      endDate = DateUtils.getCurrentMonthEndDay();
    } else if ("2".equals(assignPeriod)) {
      startDate = DateUtils.getCurrentSeasonFirstDay();
      endDate = DateUtils.getCurrentSeasonEndDay();
    } else if ("3".equals(assignPeriod)) {
      startDate = DateUtils.getCurrentYearFirstDay();
      endDate = DateUtils.getCurrentYearEndDay();
    }
    Contract contract = new Contract();
    contract.setStartDate(startDate);
    contract.setEndDate(endDate);

    List list = new ArrayList();
    list = getSqlMapClientTemplate().queryForList("getTop5ContractOrBackCostOrGrossProfit", contract);
    return list;
  }

  public Contract getContractByContractCode(String contractCode) {
    if (StringUtils.isEmpty(contractCode)) return null;
    Contract contract = new Contract();
    contract.setContractCode(contractCode);
    return (Contract)getSqlMapClientTemplate().queryForObject("getContractByDynamic", contract);
  }

  public Contract getContractById(Integer id) {
    if (id == null) return null;
    Contract contract = new Contract();
    contract.setId(id);
    return (Contract)getSqlMapClientTemplate().queryForObject("getContractByDynamic", contract);
  }

  public BigDecimal getContractCostsByCustomerIdByContractId(Integer customerId, Integer contractId) {
    Map map = new HashMap();
    map.put("customerId", customerId);
    map.put("contractId", contractId);
    return (BigDecimal)getSqlMapClientTemplate().queryForObject("getContractCostsByCustomerIdByContractId", map);
  }

  public BigDecimal getTotalContractCostByDynamic(Contract contract) {
    if (contract == null) return new BigDecimal(0);
    return (BigDecimal)getSqlMapClientTemplate().queryForObject("getTotalContractCostByDynamic", contract);
  }

  public BigDecimal getTotalContractReceivableByDynamic(Contract contract) {
    if (contract == null) return new BigDecimal(0);
    return (BigDecimal)getSqlMapClientTemplate().queryForObject("getTotalContractReceivableByDynamic", contract);
  }

  public boolean insertContract(Contract contract) {
    if (contract == null) return false;
    if (StringUtils.isEmpty(contract.getContractCode())) return false;
    Integer result = (Integer)getSqlMapClientTemplate().insert("insertContract", contract);
    Contract currContract = getContractByContractCode(contract.getContractCode());
    
    if(contract.getContractCaseList()!=null&&!contract.getContractCaseList().isEmpty()){
    	contractCaseDAO.saveContractCase(contract.getContractCaseList(), currContract.getId().longValue());
//    	for(ContractCase case1:contract.getContractCaseList()){
//    		case1.setContractId(currContract.getId().toString());
//    		try{
//    			getSqlMapClientTemplate().insert("insertContractCase",case1);	
//    		}catch(Exception e){
//    			e.printStackTrace();
//    			throw new RuntimeException();
//    		}
//    		
//    	}
    }
    return currContract != null;
  }

  public boolean updateContractByDynamic(Contract contract)
  {
    if (contract == null) return false;
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateContractByDynamic", contract));
    return result != null;
  }

  public boolean updateContractCustomerIdById(Integer customerId, Integer contractId)
  {
    if (customerId == null) return false;
    if (contractId == null) return false;
    Contract contract = new Contract();
    contract.setId(contractId);
    contract.setCustomerId(customerId);
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateContractByDynamic", contract));
    return result != null;
  }

  public boolean deleteContract(Integer id)
  {
    if (id == null) return false;
    Integer result = Integer.valueOf(getSqlMapClientTemplate().delete("deleteContract", id));
    return result != null;
  }
  
  public CodeStore getCodeStoreByType(String type)
  {
	  return (CodeStore)getSqlMapClientTemplate().queryForObject("getCodeByType",type);
  }
  
	
	public boolean insertContractCase(ContractCase contractCase)
	{
		getSqlMapClientTemplate().insert("insertContractCase", contractCase);
		return true;
	} 
	
	public List<ContractCase> getContractCaseListByCaseCode(String caseCode)
	{
		String[] caseCodes = caseCode.split("==");
		Map<String, Object> map = new HashMap<String, Object>(); 
		map.put("caseCodes", caseCodes);
		return (List<ContractCase>)getSqlMapClientTemplate().queryForList("getContractCaseListByCaseCode", map);
	}
	
	public boolean deleteContractCase(String delContractCaseId)
	{
		String[] ids = delContractCaseId.split("/");
		Map<String, Object> map = new HashMap<String, Object>(); 
		map.put("ids", ids);
		getSqlMapClientTemplate().delete("deleteContractCaseById", map);
		return true;
	}

	public void setContractCaseDAO(ContractCaseDAO contractCaseDAO) {
		this.contractCaseDAO = contractCaseDAO;
	}

	public List<Contract> getContractByDeptId(Integer userId) {
		// TODO Auto-generated method stub
		return (List<Contract>)getSqlMapClientTemplate().queryForList("getContractByDeptId",userId);
	}

	public List<Contract> getContractByUserId(Integer userId) {
		// TODO Auto-generated method stub
		return (List<Contract>)getSqlMapClientTemplate().queryForList("getContractByUserId",userId);
	}
}